﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using XiaoYuStock.Core.Entities;
namespace XiaoYuStock.Core
{
   public class DayDataService
    {
       /// <summary>
       /// 
        /// http://market.finance.sina.com.cn/transHis.php?date=2016-03-31&symbol=sh600035
       /// </summary>
       private static string requestUrl = "http://market.finance.sina.com.cn/transHis.php?date={0}&symbol={1}";

        private static Regex regTable = new Regex("<table\\s*class=\"datatbl\"\\s*id=\"datatbl\">.*?</table>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        private static Regex regTR = new Regex("<tr.*?</tr>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private static Regex regTD = new Regex("<td.*?</td>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        private static Regex regTH = new Regex("<th.*?</th>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        /// <summary>
        /// 股票代码
        /// </summary>
        public string SharesCode { get; set; }

        /// <summary>
        /// 天
        /// </summary>
        public string Date { get; set; }



        public List<TradeData> GetData()
        {
            List<TradeData> list = new List<TradeData>();
            var url = string.Format(requestUrl,  Date,this.SharesCode);
            var htmlContent = RequestHelp.Get(url, Encoding.GetEncoding("GB2312"));
            if (!string.IsNullOrWhiteSpace(htmlContent))
            {
                string tableHtml = regTable.Match(htmlContent).Value;
                if (!string.IsNullOrWhiteSpace(tableHtml))
                {
                    MatchCollection mc = regTR.Matches(tableHtml);
                    if (mc != null)
                    {
                        for (int i = 1; i < mc.Count; i++)
                        {
                            TradeData item = new TradeData();
                            var th = regTH.Matches(mc[i].Value);
                            var td = regTD.Matches(mc[i].Value);
                            if (td != null)
                            {
                                item.Time = RequestHelp.striphtml(th[0].Value).Trim();
                                item.TradePrice = Convert.ToDecimal(RequestHelp.striphtml(td[0].Value));
                                item.FloatAmount = Convert.ToDecimal(RequestHelp.striphtml(td[1].Value).Trim() == "--"?"0":RequestHelp.striphtml(td[1].Value).Trim());
                                item.TradeCount = Convert.ToInt64(RequestHelp.striphtml(td[2].Value));
                                item.TradeAmount = Convert.ToDecimal(RequestHelp.striphtml(td[3].Value));
                                item.IsBuy = RequestHelp.striphtml(th[1].Value).Trim() == "买盘";
                                list.Add(item);
                            }
                        }
                    }
                }
            }
            return list;
        }
    }
}
